package com.lykj.llw.box.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.dinuscxj.itemdecoration.LinearDividerItemDecoration
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.app.BaseApplication
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.utils.ToastUtils
import com.lykj.lib_base.net.http.CustomResponseBodyObserver
import com.lykj.lib_base.net.http.RetrofitManager
import com.lykj.llw.box.R
import com.lykj.llw.box.adapter.GameCouponAdapter
import com.lykj.llw.box.data.bean.response.GameCouponResponse
import com.lykj.llw.box.databinding.ActGameCouponBinding
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 * ================================================
 * 游戏优惠券
 * ================================================
 *
 * @author gosha
 * @date 2023/10/30-10:08
 */
class GameCouponActivity : BaseActivity() {
    lateinit var binding: ActGameCouponBinding

    private val mAdapter = GameCouponAdapter()
    private val mData = ArrayList<GameCouponResponse.DataDTO>()
    private var gid = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActGameCouponBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initUI()
        initData()
        setListener()
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar.with(this).transparentBar()
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()
    }

    override fun initUI() {
        binding.titleBar.tvTitle.text = "优惠券"
        binding.titleBar.tvRightOption.text = "我的优惠券"

        initRecyclerView()
    }


    private fun initRecyclerView() {
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        binding.rcvCoupon.layoutManager = linearLayoutManager
        binding.rcvCoupon.setHasFixedSize(true)
        val dividerItemDecoration = LinearDividerItemDecoration(
            this, LinearDividerItemDecoration.LINEAR_DIVIDER_VERTICAL
        )
        dividerItemDecoration.setDivider(
            resources.getDrawable(
                R.drawable.divider_0dp, resources.newTheme()
            )
        )
        binding.rcvCoupon.addItemDecoration(dividerItemDecoration)
        val emptyView = View.inflate(this, R.layout.empty_playing, null)
        emptyView.findViewById<TextView>(R.id.tv_desc).text = "暂无优惠券，晚点再来哦！"
        mAdapter.setEmptyView(emptyView)
        mAdapter.addChildClickViewIds(R.id.tv_coupon_receiver)
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (view.id == R.id.tv_coupon_receiver && !mData[position].isHave) {
                receiverGameCoupon("${mData[position].id}")
            }
        }
        mAdapter.data = mData
        binding.rcvCoupon.adapter = mAdapter
    }

    //获取优惠券列表
    private fun obtainGameCoupons() {
        RetrofitManager.instance.obtainGameCoupons("$gid",
            object : CustomResponseBodyObserver<ResponseBody>() {
                override fun onSubscribe(d: Disposable) {
                    addDisposables(this@GameCouponActivity, d)
                }

                override fun specificHandle(responseBody: String) {
                    val bean = GsonUtils.fromJson(responseBody, GameCouponResponse::class.java)

                    mData.clear()
                    if (bean.data.size > 0) {
                        mData.addAll(bean.data)
                    }

                    mAdapter.notifyDataSetChanged()
                }

                override fun handleException(ret: String, msg: String?) {
                    msg?.let {
                        ToastUtils.showShort(this@GameCouponActivity, it)
                    }
                }
            })
    }

    //领取游戏优惠券
    private fun receiverGameCoupon(couponId: String) {
        RetrofitManager.instance.receiverGameCoupon(couponId,
            object : CustomResponseBodyObserver<ResponseBody>() {
                override fun onSubscribe(d: Disposable) {
                    addDisposables(this@GameCouponActivity, d)
                }

                override fun specificHandle(responseBody: String) {
                    ToastUtils.showShort(this@GameCouponActivity, "领取优惠券成功")
                    obtainGameCoupons()
                }

                override fun handleException(ret: String, msg: String?) {
                    msg?.let {
                        ToastUtils.showShort(this@GameCouponActivity, it)
                    }
                }
            })
    }

    override fun initData() {
        gid = intent.getIntExtra("gid", 0)
        obtainGameCoupons()
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }

        binding.titleBar.tvRightOption.clickWithTrigger {
            if (null != BaseApplication.instance?.getUserInfo()) {
                startActivity(Intent(this, MyCouponActivity::class.java))
            } else {
                startActivity(
                    Intent(
                        this@GameCouponActivity,
                        MobileLoginActivity::class.java
                    )
                )
            }
        }
    }

    override fun onDestroy() {
        removeDisposables(this)
        super.onDestroy()
    }
}